<?php
defined('BASEPATH') OR exit('No direct script access allowed');



class Business extends MY_Controller {
    private $appKey = 'c3ea4acb181da5c4f9a46c66fce0658c';    //  你的Key
    private $appScret = '16c879fac6584b10889647cf7ee591e1';   //  你的Secret
    private $app_token_json = '{}'; //  第一次需要手动授权获取京东Token然后粘贴到这里
    public function __construct() {
		parent::__construct();
        if($this->userinfo['group']!=1&&$this->userinfo['group']!=2 ){
            $this->groupjunp($this->userinfo['group']);
        }
	}

	//主页
	public function index()	{
	    $this->load->model('Jd_users_model');
        $this->address='商户首页';
        $data['link']=$this->SetExtensionLink($this->user_id);
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/index', $data);
	}

	public function getindexdata(){
        $this->load->model('Jd_user_total_model');
        $a= $this->Jd_user_total_model->getonebyuserid($this->user_id);
        if(empty($a)){
            $a['task_nub']=0;
            $a['extension']=0;
            $a['extension_nub']=0;
        }else{
            $a=$a[0];
        }
        $a['money']=$this->userinfo['money'];
        $row['list'][0]=$a;

        $this->sendjsonmsg($row);
    }


    /**
     * 商户充值页面
     */
	public function pay(){
         $this->load->library('parser');
         $this->address='充值';
         $this->load->view('business/head');
         $this->load->view('business/left');
         $this->load->view('business/ddlb');

    }


    /**
     * 消费记录列表
     */
    public function paylog(){
        $this->address='消费记录';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/paylog');
    }


    /**
     * 获取资金详细列表
     */
    public function getpaylog(){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if(empty($epage)){
            $epage=10;
        }
        $this->load->model('Jd_pay_log_model');

        $order['id']="desc";
        $a=  $this->Jd_pay_log_model->pageData(array('compare'=>array('user_id'=>$this->user_id)),"*",$page,$epage,$order);
        $a['epage']=$epage;
        $a['page']=$page;


        foreach ($a['list'] as $id=>$v){
            $a['list'][$id]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
            switch ($v['type']){
                case 1 : $a['list'][$id]['type']='充值';break;
                case 2 : $a['list'][$id]['type']='发布任务';break;
                case 3 : $a['list'][$id]['type']='推荐奖励';break;
                case 4 : $a['list'][$id]['type']='测试员刷单';break;
                case 5 : $a['list'][$id]['type']='兑换金币';break;
                case 6 : $a['list'][$id]['type']='后台调整';break;
            }
        }


        $this->sendjsonmsg($a);

    }



    public function getgold(){
        $this->address='金币兑换';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/getgold');
    }


    /**
     * 兑换金币
     */
    public function  getgoldaction(){
        $money = $this->input->post('money');
        $gold=$money*10;
        $userinfo=$this->getcuserinfo(1);
        if($userinfo['money']<$money){
            $msg['code']="1";
            $msg['msg']="余额不足";
            $this->sendjsonmsg($msg);
        }
        $row['money']=$money;
        $row['gold']=$gold;
        $row['user_id']=$this->user_id;
        $r= $this->moneyupdate($row,5);
        if($r){
            $msg['code']=0;
            $msg['msg']='兑换成功';
            $this->ResetUserinfo();
            $this->sendjsonmsg($msg);
        }
    }
    /**
     * 商户添加充值动作
     */
    public function payaction(){

        $account = $this->input->post('account');
        $username = $this->input->post('username');
        $row['pic']=$this->input->post('pic')?$this->input->post('pic'):'';

        $money=$this->input->post('money');
        $this->load->model('Jd_pay_model');


        if(!empty($row['pic'])){
            $pic=$this->movepic($row['pic']);
            if(!$pic){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow=array();
            $newrow['user_id']=$this->user_id;
            $newrow['address']=$pic['path'];
            $newrow['url']=$pic['url'];
            $newrow['addtime']=time();
            $this->load->model('Jd_pic_model');
            $result= $this->Jd_pic_model->addpic($newrow);

            if(!$result){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic']=$result;
            $row['picurl']=$pic['url'];
        }
        $row['account']='6213903301273483';
        $row['money_maker']=$username;
        $row['money']=$money;
        $result=$this->Jd_pay_model->addpay($row);


        if(!$result){
            $msg['code'] =  1;
            $msg['msg'] =  '新增失败';
            $this->sendjsonmsg($msg);
        }
        $this->sendjsonmsg();
    }


    /**
     * 获取充值列表
     */
    public function  getpaylist(){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if(empty($epage)){
            $epage=10;
        }
        $this->load->model('Jd_pay_model');

        $order['id']="desc";
        $a=  $this->Jd_pay_model->pageData(array('compare'=>array('user_id'=>$this->user_id)),"*",$page,$epage,$order);
         foreach ($a['list'] as $id=>$v){
             $a['list'][$id]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
             switch ($v['status']){
                 case 0 : $a['list'][$id]['status']='未审核';break;
                 case 1 : $a['list'][$id]['status']='已通过';break;
                 case 2 : $a['list'][$id]['status']='被拒绝';break;
             }

         }
        $a['epage']=$epage;
        $a['page']=$page;
        $this->sendjsonmsg($a);
    }


    /**
     * 京东任务添加页面
     */
    public  function  jdtask(){
        $this->load->library('parser');
        $this->address='任务发布';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/jdtask');
    }


    public function pddtask(){


       // ECHO phpinfo(); exit;
        $this->load->library('parser');
        $this->address='拼多多任务发布';



        $this->load->helper(array('form', 'url'));
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/pddtask');
    }


    /**
     * 京东任务管理
     */
    public function jdtasklist(){
        $this->load->library('parser');
        $this->address='任务管理';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/jdtasklist');
    }


    /**
     * 查看任务
     */
    public function viewjdtask($id){
        $this->address='查看任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row=array();
        $this->load->model('Jd_task_jd_model');
        $row=$this->Jd_task_jd_model->getonebyid($id);
        $this->load->view('business/viewjdtask',$row);
    }

    public function viewpddtask($id){
        $this->address='查看任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row=array();
        $this->load->model('Jd_task_jd_model');
        $row=$this->Jd_task_jd_model->getonebyid($id);
        $this->load->view('business/viewpddtask',$row);
    }



    public function relodjdtask($id){
        $this->address='重新发布任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row=array();
        $this->load->model('Jd_task_jd_model');
        $row=$this->Jd_task_jd_model->getonebyid($id);
        $str=explode( "/",$row['picurl']);
        if(  isset($str[1]) && $str[1]=='pic'){
            $row['picurl']=base_url()."public/uploads". $row['picurl'];
        }
        $this->load->view('business/relodjdtask',$row);
    }



    /**
     * 重新发布任务
     */
    public function againtask(){
        $id = $this->input->post('id');
        $this->load->model('Jd_task_jd_model');

        $row=$this->Jd_task_jd_model->getonebyid($id);

        $array[]='link';
        $array[]='goods_name';
        $array[]='price';
        $array[]='pay_price';
        $array[]='number';
        $array[]='each_single';
        $array[]='keyword';
        $array[]='coupon';
        $array[]='sku';
        $array[]='remarks';
        $array[]='shop_name';

        foreach ($row as $id=>$v){
            if(!in_array($id,$array)){
              unset($row[$id]);
            }
        }
        $r=$this->SendJdTask($row);
    }



    public function GetPDDTaskList(){
        $this->GetJdTaskList(2);
    }

    public function GetJdTaskList( $type=1){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if(empty($epage)){
            $epage=10;
        }
        $this->load->model('Jd_task_jd_model');

        $order['id']="DESC";
        $where="business_id=$this->user_id AND type=$type";

        $a=  $this->Jd_task_jd_model->pageData($where,"*",$page,$epage,$order);

       // echo $this->db->last_query();die;

        $a['epage']=$epage;
        $a['page']=$page;



        foreach ($a['list'] as $id=>$v){
            $a['list'][$id]['money']=$v['pay_price']* $v['number']* $v['each_single'];
            //$a['list'][$id]['gold']= $this->getcommission( $a['list'][$id]['money'],$this->user_id);

            $a['list'][$id]['number']=$v['finish_nub']."/". $v['number'];
            switch ($v['status']){
                case 0 : $a['list'][$id]['status']='未发布';break;
                case 1 : $a['list'][$id]['status']='已发布';break;
                case 2 : $a['list'][$id]['status']='已发布';break;
                case 3 : $a['list'][$id]['status']='已完成';break;
            }
            if(!empty($v['pay_type'])){
                switch ($v['pay_type']){
                    case 1 : $a['list'][$id]['pay_type']='全部开团';break;
                    case 2 : $a['list'][$id]['pay_type']='单独购买';break;
                    case 3 : $a['list'][$id]['pay_type']='优先开团';break;
                }
            }
        }
        $this->sendjsonmsg($a);

    }


    /**
     * 拼多多任务列表
     */
    public function  pddtasklist(){
        $this->address='拼多多任务';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $this->load->view('business/pddtasklist');

    }


    /**
     * 任务单号列表
     */
    public function order_list($taskid){
        $this->load->library('parser');
        $this->address='任务详情';
        $data['taskid']=$taskid;
        $this->load->view('business/head',$data);
        $this->load->view('business/left');
        $this->load->view('business/order_list',$data);
    }


    public function relodpddtask($id){
        $this->address='拼多多任务重新发布';
        $this->load->view('business/head');
        $this->load->view('business/left');
        $row=array();
        $this->load->model('Jd_task_jd_model');
        $row=$this->Jd_task_jd_model->getonebyid($id);
        $str=explode( "/",$row['picurl']);
        if(  isset($str[1]) && $str[1]=='pic'){
            $row['picurl']=base_url()."public/uploads". $row['picurl'];
        }
        $this->load->view('business/relodpddtask',$row);
    }




    /**
     * 获取任务单号详情列表
     */
    public function GetJdOrderList(){
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        $taskid=$this->input->post('taskid');
        $orderid=$this->input->post('orderid')?$this->input->post('orderid'):"";
        $orderid=trim($orderid);
        if(empty($epage)){
            $epage=10;
        }



        $row=array('compare'=>array('business_id'=>$this->user_id,'taskid'=>$taskid));
        if($orderid){
            $row['compare']['orderid']=$orderid;
        }
        $this->load->model('Jd_task_jd_list_model');
        $a=  $this->Jd_task_jd_list_model->pageData($row,"*",$page,$epage);
        $a['epage']=$epage;
        $a['page']=$page;
        foreach ($a['list'] as $id=>$v){
            if($v['status']==2){
                $a['list'][$id]['status']="完成";
                $a['list'][$id]['finishtime']= date("Y-m-d H:i:s",$v['finishtime']);

            }else{
                $a['list'][$id]['finishtime']="";
                $a['list'][$id]['status']="未完成";
                $a['list'][$id]['account']="";
                $a['list'][$id]['real_price']="";
                $a['list'][$id]['orderid']="";
                $a['list'][$id]['hunter_name']="";
            }
        }
        $this->sendjsonmsg($a);

    }

    public function getgoldbymoney($perject=1){
        $allmoney= $this->input->post("needmoney");
        $allgold=$this->getcommission($allmoney,$this->user_id,$perject);
        $msg['code'] =  0;
        $msg['msg'] =  '获取成功';
        $msg['allgold'] = $allgold;
        $this->sendjsonmsg($msg);
    }



    /**
     * 商户发布京东任务
     */
    public function  SendJdTask($type=1){
        if(empty($row)){
            $row= $this->input->post();
        }

        foreach ($row as $id=>$v){
            $row[$id]=trim($v);
        }

        $this->load->model('Jd_task_jd_model');

        if(!empty($row['pic'])){
            $pic=$this->movepic($row['pic']);
            if(!$pic){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow=array();
            $newrow['user_id']=$this->user_id;
            $newrow['address']=$pic['path'];
            $newrow['url']=$pic['url'];
            $newrow['addtime']=time();
            $this->load->model('Jd_pic_model');
            $result= $this->Jd_pic_model->addpic($newrow);

            if(!$result){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic']=$result;
            $row['picurl']=$pic['url'];
        }
        if(!isset($row['picurl'])){
            $row['picurl']="";
        }
        $row['type']=$type;//默认为京东任务
        foreach ($row as $id=>$v){
            if(empty($v) && $id!="coupon" && $id!="remarks"&& $id!="pic"&& $id!="sku"&& $id!="picurl"){
                $msg['code'] =  1;
                $msg['msg'] =  '缺少参数';
                $this->sendjsonmsg($msg);
            }
        }


        $allmoney=$row['pay_price']*$row['each_single'];
        $allgold=$this->getcommission($allmoney*$row['number'],$this->user_id,$type);
        $allmoney=$allmoney*$row['number'];
        $result=$this->checkmoney($allmoney,$allgold);
        if(!$result){
            $msg['code'] =  1;
            $msg['msg'] =  '金币或者余额不足';
            $this->sendjsonmsg($msg);
        }
        $row['gold']=$allgold;
        $this->db->trans_begin();
        $result=  $this->Jd_task_jd_model->addtask($row);
        if($result){
            $array=array();
            $array['taskid']=$result;
            $array['list']=$row['number'];
            $array['price']=$row['pay_price'];
            $array['goods_name']=$row['goods_name'];
            $array['shop_name']=$row['shop_name'];
            $array['number']=$row['each_single'];
            $array['link']=$row['link'];
            $array['keyword']=$row['keyword'];
            $array['remarks']=$row['remarks'];
            $array['coupon']=$row['coupon'];
            $array['sku']=$row['sku'];
            $array['type']=$type;
            $array['picurl']=$row['picurl'];
            $this->load->model('Jd_task_jd_list_model');
            $result=  $this->Jd_task_jd_list_model->addtasklist($array);
        }else{
            $this->db->trans_rollback();
            $msg['code'] =  1;
            $msg['msg'] =  '添加任务失败';
            $this->sendjsonmsg($msg);
        }
        if($result){//如果任务发布成功即开始
            $newrow['task_nub']=$row['number'];
            $this->UpdataUserinfo($newrow,$this->user_id);
            $newrow=array();
            $newrow['money']=$allmoney;
            $newrow['gold']=$allgold;
            $newrow["user_id"]=$this->user_id;
            $result=$this->moneyupdate($newrow,2,$result);
        }else{
            $this->db->trans_rollback();
            $msg['code'] =  1;
            $msg['msg'] =  '子任务发布失败';
            $this->sendjsonmsg($msg);
        }
         if(!$result){
             $this->db->trans_rollback();
             $msg['code'] =  1;
             $msg['msg'] =  '任务信息日志更新失败';
             $this->sendjsonmsg($msg);
         }
        $this->db->trans_commit();
        $msg['code'] =  0;
        $msg['msg'] =  '任务发布成功';

        $this->ResetUserinfo();
        $this->sendjsonmsg($msg);

    }


    /**
     * 发布拼多多任务
     * @param array $row
     */
    public function SendPddTask($row=array()){
        if(empty($row)){
            $row= $this->input->post();
        }

        foreach ($row as $id=>$v){
            $row[$id]=trim($v);
        }
        if(!empty($row['pic'])){//上传图片
            $pic=$this->movepic($row['pic']);
            if(!$pic){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $newrow=array();
            $newrow['user_id']=$this->user_id;
            $newrow['address']=$pic['path'];
            $newrow['url']=$pic['url'];
            $newrow['addtime']=time();
            $this->load->model('Jd_pic_model');
            $result= $this->Jd_pic_model->addpic($newrow);
            if(!$result){
                $msg['code'] =  1;
                $msg['msg'] =  '图片上传失败';
                $this->sendjsonmsg($msg);
            }
            $row['pic']=$result;
            $row['picurl']=$pic['url'];
        }

        $this->load->model('Jd_task_jd_model');
        $row['type']=2;
        foreach ($row as $id=>$v){
            if(empty($v) && $id!="coupon" && $id!="remarks"&& $id!="pic"){
                $msg['code'] = 1;
                $msg['msg'] = '缺少参数';
                $this->sendjsonmsg($msg);
            }
        }

        $allmoney=$row['pay_price']*$row['each_single'];
        $allgold=$this->getcommission($allmoney*$row['number'],$this->user_id);
        $allmoney=$allmoney*$row['number'];
        $result=$this->checkmoney($allmoney,$allgold);
        if(!$result){
            $msg['code'] = 1;
            $msg['msg'] = '金币或者余额不足';
            $this->sendjsonmsg($msg);
        }
        $row['gold']=$allgold;
        $this->db->trans_begin();



        $result=  $this->Jd_task_jd_model->addtask($row);
        if($result){
            $array=array();
            $array['taskid']=$result;
            $array['list']=$row['number'];
            $array['price']=$row['pay_price'];
            $array['goods_name']=$row['goods_name'];
            $array['shop_name']=$row['shop_name'];
            $array['number']=$row['each_single'];
            $array['link']=$row['link'];
            $array['pay_type']=$row['pay_type'];
            $array['single_price']=$row['single_price'];
            $array['group_price']=$row['group_price'];
            $array['remarks']=$row['remarks'];
            $array['keyword']=$row['keyword'];
            $array['sku']=$row['sku'];
            $array['type']=2;

            if(isset($row['picurl'])){
                $array['picurl']=isset($row['picurl'])?$row['picurl']:"";
            }
            $this->load->model('Jd_task_jd_list_model');
            $result=  $this->Jd_task_jd_list_model->addtasklist($array);
        }else{
            $this->db->trans_rollback();
            $msg['code'] =  1;
            $msg['msg'] =  '添加任务失败';
            $this->sendjsonmsg($msg);
        }
        if($result){//如果任务发布成功即开始
            $newrow['task_nub']=$row['number'];
            $this->UpdataUserinfo($newrow,$this->user_id);
            $newrow=array();
            $newrow['money']=$allmoney;
            $newrow['gold']=$allgold;
            $newrow["user_id"]=$this->user_id;


            $result=$this->moneyupdate($newrow,2,$result);
        }else{
            $this->db->trans_rollback();
            $msg['code'] =  1;
            $msg['msg'] =  '子任务发布失败';
            $this->sendjsonmsg($msg);
        }
        if(!$result){
            $this->db->trans_rollback();
            $msg['code'] =  1;
            $msg['msg'] =  '任务信息日志更新失败';
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_commit();
        $msg['code'] =  0;
        $msg['msg'] =  '任务发布成功';

        $this->ResetUserinfo();
        $this->sendjsonmsg($msg);

    }


    /**
     * 通过链接获取商品信息（京东）
     */
    public  function  getgoodinfo(){
        // $data['skuIds'] = '5225346,7275691';
        $link= $this->input->post('link');
        $subject='%.*?item.jd.com\\/(\d{0,20})%is';
        $result= preg_match($subject,$link,$row);
        if(!$result){
            $msg['code']=1;
            $msg['msg']="链接格式错误";
            $this->sendjsonmsg($msg);
        }
        $skuIds=$row[1];
        $data['skuIds'] =$skuIds;
        $param['param_json'] = json_encode($data);
      //  $GoodsList = $this->sign_method('jd.union.open.goods.promotiongoodsinfo.query',$param);
        $GoodsList = $this->sign_method('jd.union.open.goods.promotiongoodsinfo.query',$param);
        $GoodsList = $this->curl_post('https://router.jd.com/api', $GoodsList);
        $GoodsList = json_decode($GoodsList,true);
        if($GoodsList['jd_union_open_goods_promotiongoodsinfo_query_response']['code']!=0){
            $msg['code']=1;
            $msg['msg']="接口请求失败 请自行输入参数";
            $this->sendjsonmsg($msg);
        }
        $result=$GoodsList['jd_union_open_goods_promotiongoodsinfo_query_response']['result'];
        $result=json_decode($result,true);
        $result=$result['data'][0];
        $result['code']=0;
        $this->sendjsonmsg($result);
    }


    function sign_method($method,$data=array()){
        date_default_timezone_set("PRC");
        $sign_method = array(
            'app_key'=> $this->appKey,
            'timestamp'=> date('Y-m-d H:i:s'),
            'format' => 'json',
            'method' => $method,
            'v' => '1.0',
            'sign_method'=>'md5',
        );
        $sign_method = array_merge($sign_method,$data);
        ksort($sign_method);
        // var_dump($array_merge);die();
        $data =  $this->appScret;
        foreach ($sign_method as $key => $value) {
            $data .= trim($key) .trim($value);
        }
        $data .= $this->appScret;

       // echo $data; exit;
        $sign_method['sign'] = strtoupper(md5($data));
        // echo "<pre>";
        // var_dump($sign_method);die();
        return $sign_method;
    }
    // post请求
    function curl_post($url, $curlPost)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


    function GetOrderListexcel($taskid){
        $this->load->model('Jd_task_jd_list_model');
        $page=1;
        $epage=100000;
        $where="taskid= $taskid "  ;
        $select="id,finishtime,shop_name,keyword,price,number,orderid,sku,hunter_name,type,real_price,`status`";
        $a=  $this->Jd_task_jd_list_model->pageData($where,$select,$page,$epage);
        $a['epage']=$epage;
        $a['page']=$page;
        foreach ($a['list'] as $id=>$v){
            if($v['status']==0){
                $a['list'][$id]['status']="未完成";
                $a['list'][$id]['finishtime']="";
            }else{
                if($v['status']==1){
                    $a['list'][$id]['status']="执行中";
                    $a['list'][$id]['finishtime']="";
                }else{
                    $a['list'][$id]['status']="已完成";
                    $a['list'][$id]['finishtime']= date("Y-m-d H:i:s",$v['finishtime']);
                }
            }
            if($v['type']==1){
                $a['list'][$id]['type']="京东";
            }else{
                $a['list'][$id]['type']="拼多多";
            }
        }
        $str[]='ID';
        $str[]='完成时间';
        $str[]='店铺名';
        $str[]='关键字';
        $str[]='录入价格';
        $str[]='每单数量';
        $str[]='订单号';
        $str[]='SKU';
        $str[]='测试员';
        $str[]='类型';
        $str[]='实际付款';
        $str[]='状态';
        $this->excel_out($str, $a['list'],"任务".$taskid."订单记录");
    }















}




